﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PM.Extractor
{
    internal class Extractor
    {
        private Dictionary<string, FileInfo> _files = new Dictionary<string, FileInfo>();
        private DirectoryInfo _rootFolder = null;

        public Extractor(DirectoryInfo rootFolder)
        {
            _rootFolder = rootFolder;
        }

        public void GetAllCompressedFiles()
        {
            foreach (FileInfo files in _rootFolder.GetFiles())
            {
                ProcessAllSubfiles(files);
            }

            foreach (DirectoryInfo dirs in _rootFolder.GetDirectories())
            {
                ProcessAllSubFolders(dirs);
            }
        }

        private void GetAllCompressedFiles(DirectoryInfo dir)
        {
            foreach (FileInfo files in dir.GetFiles())
            {
                ProcessAllSubfiles(files);
            }

            foreach (DirectoryInfo dirs in dir.GetDirectories())
            {
                ProcessAllSubFolders(dirs);
            }
        }

        internal void ProcessAllSubFolders(DirectoryInfo dirs)
        {
            GetAllCompressedFiles(dirs);
        }

        private void ProcessAllSubfiles(FileInfo file)
        {
            string[] exts = System.Configuration.ConfigurationSettings.AppSettings["CompressedExtensions"].Split(';');

            foreach (string s in exts)
            {
                if (file.Extension == "."+s)
                {
                    _files.Add(file.FullName, file);               
                }
            }
        }

        public void ExtractCompressFiles()
        {
            foreach (string path in _files.Keys)
            {
                FileInfo file = _files[path];

                StartNewExtractProcess(file);
            }
        }

        private void StartNewExtractProcess(FileInfo file)
        {
            Console.WriteLine("Going to extract:" + file.FullName);
            try
            {
                string command = null;
                
                if(System.Configuration.ConfigurationSettings.AppSettings["Mode"] == "0")
                {
                    command = string.Format(@"x {0} -o""{1}"" -y", file.FullName, file.Directory.FullName + "\\\\" + file.Name.Substring(0, file.Name.LastIndexOf('.')));
                }
                else
                {
                    DirectoryInfo outputFolder = new DirectoryInfo(System.Configuration.ConfigurationSettings.AppSettings["OutputFolder"]);
                    if (!outputFolder.Exists)
	                {
		                throw new DirectoryNotFoundException();
	                }

                    command = string.Format(@"x {0} -o""{1}"" -y", file.FullName, outputFolder.FullName);
                }

                System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("7z.exe"
                    , command);
                procStartInfo.RedirectStandardOutput = true; procStartInfo.UseShellExecute = false;
                procStartInfo.CreateNoWindow = true;
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();
                string result = proc.StandardOutput.ReadToEnd();
                // Display the command 
                if (!result.Contains("Everything is Ok"))
                {
                    Console.WriteLine("SOMETHING WENT WRONG:\r\n"+result);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("Extracted.");
        }
    }
}
